package com.ynet.http;

import java.util.Map;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

import com.alibaba.fastjson.JSONObject;
import com.ynet.utils.HttpClientUtil;

public class ForReq extends Thread{

	private static String userName;
	private static String password;
	private static String innerUrl;
	private static String wanUrl;
	
	public ForReq(){}
	
	public ForReq(String userName, String password, String innerUrl, String wanUrl){
		ForReq.userName = userName;
		ForReq.password = password;
		ForReq.innerUrl = innerUrl;
		ForReq.wanUrl = wanUrl;
	}
	
	public static void main(String[] args){
		
//		new ForReq().logon("zhangsan", "zhangsan", "http://baidu.com");
		String wanUrl = "http://baidu.com/";
		int wanUrlLength = wanUrl.length();
		wanUrl = wanUrl.substring(0, wanUrlLength-1);

//		System.out.println(wanUrl);
		
	}
	
	/**
	 * 登陆 TODO
	 * http://127.0.0.1:18080/penetrate-server/logon.inner
	 * return true 登陆成功  false 登陆失败
	 * 
	 */
	public Map<String, Object> logon(String userName, String password, String wanUrl){
		
		String appid = userName;
		
		String url = wanUrl + "logon.inner/";
		String json = "{" + "\"appid\":\"" + appid + "\"," + "\"password\":\"" + password + "\"" + "}";
		
		String resultString = HttpClientUtil.doPostJson(url, json);
		JSONObject obj = JSONObject.parseObject(resultString);
		
		return obj;
	}
	
	/**
	 * 注册
	 */
	public Map<String, Object> register(String userName, String password, String password2, String wanUrl){
		
		String appid = userName;
		
		if(wanUrl != null && !wanUrl.equals("")){
			if(wanUrl.endsWith("/")){
				int wanUrlLength = wanUrl.length();
				wanUrl = wanUrl.substring(0, wanUrlLength-1);
			}
		}
		
		String url = wanUrl + "/register.inner/";
		String json = "{" + "\"appid\":\"" + appid + "\"," + "\"password\":\"" + password + "\"" + "}";
		
		String resultString = HttpClientUtil.doPostJson(url, json);
		
		JSONObject obj = JSONObject.parseObject(resultString);
		
		return obj;
	}
	
	
	/**
	 * 监听请求
	 * @param userName    
	 * @param password	  
	 * @param innerUrl    "http://127.0.0.1:58080/fm-gateway/"
	 * @param wanUrl      "http://127.0.0.1:18080/penetrate-server/"
	 */
	public void run(){
//		System.err.println("ok");
		for(int i = 0; i < 2000; i++){

			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			String appid = ForReq.userName;
			
			String url = wanUrl + appid + ".lan/";
			//循环请求服务器
			String resultString = HttpClientUtil.doPostJson(url, "{}");
			System.out.println("第" + i + "次请求： " + resultString);
			
			if(resultString == null || "".equals(resultString)){continue ;}
			
			JSONObject json = JSONObject.parseObject(resultString);
			
			if(json != null && json.get("returnCode").equals("999999")){
				JOptionPane.showMessageDialog(new JPanel(), "链接异常，请检查服务器地址", "警告",JOptionPane.WARNING_MESSAGE);  
				break;
			}
			
			String id = (String) json.get("id");
			String method = (String) json.get("method");//get post
			String reqData = (String) json.get("req_data");
			String dataType = (String) json.get("data_type");//json from
			String lan_url = (String) json.get("lan_url");//    /userData/registNum&bsUserId=1&datav=1
//		
//			System.out.println("	获得 id： " + id);
//			System.out.println("	获得 method： " + method);
//			System.out.println("	获得 reqData： " + reqData);
//			System.out.println("	获得 dataType： " + dataType);
//			System.out.println("	获得 lan_url： " + lan_url);

			//将响应参数返回给服务器
			String serverUrl = wanUrl + appid + ".reswan" + lan_url;
			
			String responseStr = "";//响应参数
			if ("post".equalsIgnoreCase(method) && "json".equalsIgnoreCase(dataType)) {
				responseStr = HttpClientUtil.doPostJson(innerUrl + lan_url, reqData);
				
//				System.out.println("响应URL ： " + serverUrl);
//				System.out.println("响应data： " + responseStr);
				
				responseStr = HttpClientUtil.doPostJson(serverUrl, responseStr);
			} else if("post".equalsIgnoreCase(method) && "from".equalsIgnoreCase(dataType)) {
				responseStr = HttpClientUtil.doPost(innerUrl + lan_url, JSONObject.parseObject(reqData));
				responseStr = HttpClientUtil.doPost(serverUrl, JSONObject.parseObject(responseStr));
			} else if("get".equalsIgnoreCase(method) && "from".equalsIgnoreCase(dataType)){
				responseStr = HttpClientUtil.doGet(innerUrl + lan_url, JSONObject.parseObject(reqData));
				responseStr = HttpClientUtil.doGet(serverUrl, JSONObject.parseObject(responseStr));
			}
			
		}
		
	
	}
}
